
#ifdef WIN32
	#include "Windows.h"
#else        //Linux
	#include <sys/time.h>
	#include <time.h>
#endif

#include <stdlib.h>
#include <cmath>
#include <iostream>
using namespace std;

const double SPLIT = 0.01;
const int COUNT = 200;
const double PI = 3.14159265;
const int INTERVAL = 300;

int main()
{
	unsigned long busySpan[COUNT],idleSpan[COUNT];
	const int half = INTERVAL/2;
	for(unsigned i = 0; i < COUNT; i++)
	{
		busySpan[i] = half + sin(i*2*PI/COUNT)*half;
		idleSpan[i] = INTERVAL - busySpan[i];
	}

	unsigned long startTime = 0;
	int j = 0;
	while(true)
	{
		j = j % COUNT;
		
#ifdef WIN32
		startTime = GetTickCount();
		while(GetTickCount() - startTime <= busySpan[j])
			;
		Sleep(idleSpan[j]);
#else
		struct timeval tv;
		if(gettimeofday(&tv,NULL) == 0)
		{
			startTime = tv.tv_sec*1000 + tv.tv_usec/1000;
		}
		unsigned long curTime = startTime;
		while(curTime - startTime <= busySpan[j])
		{
			if(gettimeofday(&tv,NULL) == 0)
				curTime = tv.tv_sec*1000 + tv.tv_usec/1000;
		}

		struct timespec timeOut, remains;
		timeOut.tv_sec = 0;
		timeOut.tv_nsec = idleSpan[j]*1000000;
		nanosleep(&timeOut,&remains);
#endif
		j++;
	}
	return 0;
}
